#!/usr/bin/env python
from models import *
import random
from grade import *
import json



"""
{"migrate_c_threshold": 9, "migrate_m_threshold": 9, "migrate_signal_max_threshold": 80, "migrate_double_max_threshold": 40, "put_c_threshold": 9, "put_m_threshold": 9, "put_signal_max_threshold": 76, "put_double_max_threshold": 46, "leftmargin": 0.06, "rightmargin": 0.17, "cost": 1120868742}"""
if __name__ == '__main__':
    epochs = 0
    f = open('best6.json', 'w')
    min_cost = 99999999999

    (9, 9, 80, 40, 9, 9, 76, 46, 0.06, 0.17)

    while True:
        epochs+=1
        print("epochs",str(epochs))
        migrate_c_threshold = 9#random.randint(0, 20)
        migrate_m_threshold = 9#random.randint(0, 20)
        migrate_signal_max_threshold = 80   #random.randint(50, 100)
        migrate_double_max_threshold = 40   #random.randint(50, 100)
        put_c_threshold = random.randint(0, 20)
        put_m_threshold = random.randint(0, 20)
        put_signal_max_threshold = 76#random.randint(40, 90)
        put_double_max_threshold = 46#random.randint(40, 90)
        leftmargin = 0.06#round(random.uniform(0,0.3),2)
        rightmargin = 0.17#round(random.uniform(0,0.3),2)
        parameter1 = parameter(migrate_c_threshold,migrate_m_threshold,migrate_signal_max_threshold,migrate_double_max_threshold,
                  put_c_threshold,put_m_threshold,put_signal_max_threshold,put_double_max_threshold,leftmargin,rightmargin)

        # 判题器的位置
        set_up_log(False)
        # name = input('给本次运行起个名字，便于调参: ')
        if platform.system().startswith('L'):
            executable = '../../../judge'
        else:
            executable = r'..\..\..\judge.exe'
        input1 = '../../../data/training-1.txt'
        input2 = '../../../data/training-2.txt'
        if not os.path.exists(executable):
            l.fatal('no such file: %s' % (executable))
            sys.exit(-1)

        # 生成两个临时文件
        _, output1 = tempfile.mkstemp()
        _, output2 = tempfile.mkstemp()
        print('output1: %s' % (output1))
        print('output2: %s' % (output2))

        # 计时
        timer = Timer()
        ServerInstance.instance_index = 0
        print('Running on input1...')
        run_strategy(input1, output1, parameter1)
        ServerInstance.instance_index = 0
        print('Running on input2...')
        run_strategy(input2, output2, parameter1)
        print('Time used: %.2fs' % timer.elapsed())

        cost1, migration1 = calculate(executable, input1, output1)
        cost2, migration2 = calculate(executable, input2, output2)
        cost = cost1+cost2
        print('Cost: %d=%d+%d' % (cost1 + cost2, cost1, cost2))
        print('Migration: %d=%d+%d' % (migration1 + migration2, migration1, migration2))
        if(cost<min_cost):
            min_cost = cost
            parameter1.cost = cost
            try:
                f.write(json.dumps(parameter1.__dict__))
                f.write("\n")
                f.flush()
            except Exception:
                f.close()

    # 算钱

    os.remove(output1)
    os.remove(output2)